package com.book.service.Impl;

import com.book.dao.Impl.bookDaoImpl;
import com.book.dao.bookDao;
import com.book.pojo.Book;
import com.book.pojo.Page;
import com.book.service.bookService;

import java.util.List;

public class bookServiceImpl implements bookService {
    bookDao bookDao = new bookDaoImpl();

    /**
     * 添加图书
     *
     * @param book
     */
    @Override
    public void addBook(Book book) {
        String sql = "insert into t_book values(?,?,?,?,?,?,?)";
        bookDao.addBook(new Book(book.getId(), book.getName(), book.getPrice(), book.getAuthor(), book.getSales(), book.getStock(), book.getImgPath()));
    }

    /**
     * 更新图书
     *
     * @param book
     */
    @Override
    public void updateBook(Book book) {
        String sql = "update t_book set id=?,name=?,price=?,author=?,sales=?,stock=?,img_path=? where id=?";
        bookDao.updateBook(new Book(book.getId(), book.getName(), book.getPrice(), book.getAuthor(), book.getSales(), book.getStock(), book.getImgPath()));
    }

    /**
     * 根据id删除图书
     *
     * @param id
     */
    @Override
    public void delBookById(Integer id) {
        String sql = "delete from t_book where id =?";
        bookDao.delBookById(id);
    }

    /**
     * 根据id查询图书
     *
     * @param id
     * @return 查询的图书对象
     */
    @Override
    public Book queryBookById(Integer id) {
        return bookDao.queryBookById(id);
    }

    /**
     * 查询所有的图书
     *
     * @return 所有的图书bean对象
     */
    @Override
    public List<Book> queryBooks() {
        return bookDao.queryBooks();
    }

    @Override
    public Page<Book> page(int pageNo, int pageSize) {
        Page<Book> page = new Page<>();
        page.setPageSize(pageSize);
        //总记录数
        Integer count = bookDao.queryForPageCount();
        page.setPageTotalCount(count);
        //总页码
        Integer pageTotal = count / pageSize;
        if(count%pageSize>0){
            pageTotal+=1;
        }
        page.setPageTotal(pageTotal);
        page.setPageNo(pageNo);
        // 当前页数据
        int begin=(page.getPageNo()-1)*pageSize;
        List<Book> items=bookDao.queryForPageItems(begin,pageSize);
        page.setItems(items);
        return page;
    }
}
